package com.cskaoyan.service;

import com.cskaoyan.bean.data.CartCheckoutData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.OrderSubmitVo;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author zoe ling
 * @Date 2024/6/14
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    MarketOrderMapper orderMapper;

    @Autowired
    OrderGoodsService orderGoodsService;
    @Autowired
    CartService cartService;
    @Autowired
    ProductService productService;
    @Autowired
    CouponUserService couponUserService;
    @Autowired
    AddressService addressService;


    @Override
    @Transactional
    public Integer sumbit(Integer userId, OrderSubmitVo submitVo) {
        Integer addressId = submitVo.getAddressId();
        Integer cartId = submitVo.getCartId();
        Integer couponId = submitVo.getCouponId();
        Integer userCouponId = submitVo.getUserCouponId();
        String message = submitVo.getMessage();
        Date current = new Date();
        long l = System.currentTimeMillis();
        //增加事务
        //1.添加marketorder表中的数据
        MarketAddress address=addressService.findById(addressId);
        MarketCouponUser couponUser=couponUserService.findById(userCouponId);
        String detailAddress= address.getProvince()+address.getCity()+ address.getCounty()+address.getAddressDetail();
        CartCheckoutData data = cartService.checkout(userId, cartId, addressId, couponId, userCouponId);
        String orderSn = "" + userId + current + l;
        MarketOrder marketOrder = MarketOrder.builder()
                .userId(userId)
                .orderSn(orderSn)
                .orderStatus((short) 101)
                .consignee(address.getName())
                .mobile(address.getTel())
                .address(detailAddress)
                .message(message)
                .goodsPrice(data.getGoodsTotalPrice())
                .freightPrice(data.getFreightPrice())
                .couponPrice(data.getCouponPrice())
                .orderPrice(data.getOrderTotalPrice())
                .actualPrice(data.getActualPrice())
                .addTime(current)
                .updateTime(current)
                .build();
        orderMapper.insertSelective(marketOrder);
        MarketOrderExample orderExample = new MarketOrderExample();
        orderExample.createCriteria()
                .andUserIdEqualTo(userId)
                .andOrderSnEqualTo(orderSn)
                .andDeletedEqualTo(false);
        List<MarketOrder> marketOrders = orderMapper.selectByExample(orderExample);
        if (marketOrders!=null&&marketOrders.size()==1){
            MarketOrder order = marketOrders.get(0);
            Integer orderId = order.getId();
            //2.添加marketordergoods表中的数据
            if (cartId!=0){
                MarketCart marketCart=cartService.findById(cartId);
                Integer goodsId = marketCart.getGoodsId();
                Short orderNumber = marketCart.getNumber();
                MarketOrderGoods orderGoods = MarketOrderGoods.builder()
                        .orderId(orderId)
                        .goodsId(goodsId)
                        .goodsName(marketCart.getGoodsName())
                        .goodsSn(marketCart.getGoodsSn())
                        .productId(marketCart.getProductId())
                        .number(orderNumber)
                        .price(marketCart.getPrice())
                        .specifications(marketCart.getSpecifications())
                        .picUrl(marketCart.getPicUrl())
                        .addTime(current)
                        .updateTime(current)
                        .build();
                orderGoodsService.add(orderGoods);
                //3.在marketgoodsproduct表中减去商品数量--更新操作
                productService.deduceGoods(goodsId, orderNumber);
                //4.删除购物车中记录marketCart
                cartService.delById(cartId);
                //5.将使用的优惠券在marketusercoupon中删除
                couponUserService.delById(userCouponId);
                return orderId;
            }else {
                //2.添加marketordergoods表中的数据
                //cartId==0---购物车中选中
                List<MarketCart> checkedGoodsList = data.getCheckedGoodsList();
                //购物车中选中的商品
                List<Integer> cartIdList = checkedGoodsList.stream().filter(MarketCart::getChecked).map(MarketCart::getId).toList();
                for (Integer id : cartIdList) {
                    MarketCart marketCart=cartService.findById(id);
                    Integer goodsId = marketCart.getGoodsId();
                    Short orderNumber = marketCart.getNumber();
                    MarketOrderGoods orderGoods = MarketOrderGoods.builder()
                            .orderId(orderId)
                            .goodsId(goodsId)
                            .goodsName(marketCart.getGoodsName())
                            .goodsSn(marketCart.getGoodsSn())
                            .productId(marketCart.getProductId())
                            .number(orderNumber)
                            .price(marketCart.getPrice())
                            .specifications(marketCart.getSpecifications())
                            .picUrl(marketCart.getPicUrl())
                            .addTime(current)
                            .updateTime(current)
                            .build();
                    orderGoodsService.add(orderGoods);
                    //3.在marketgoodsproduct表中减去商品数量--更新操作
                    productService.deduceGoods(goodsId, orderNumber);
                    //4.删除购物车中记录marketCart
                    cartService.delById(id);
                    //5.将使用的优惠券在marketusercoupon中删除
                    couponUserService.delById(userCouponId);
                    return orderId;

                }

            }


        }

        return null;
    }

}
